
	var base_url = "http://" + window.location.hostname + "/";
	var controller = window.location.pathname.split('/')[2];
	
	var type = null;
	
	var effect = 'slide';
	var effect_options = {};
    var effect_speed = 500;
    
    var editor = '';
    var editor_id = '';
    var editor_data = '';
    
    var ajax_post_param = { };
    
    function do_ajax_post_by_condition(condition, ajax_post_param) {
		switch (condition) {
			case 0:				
				$.post(base_url + "index.php/" + controller + "/common", { type: type, request: 'main_page' },
				function (html) {
					window.location.hash = '#' + type;
					
					$().containerSimulator({ destory: true });
					
					switch (controller) {
						case 'project':
						case 'service':
						case 'media':
							$("#content > div.row").html(html).effect(effect, effect_options, effect_speed);
							break;
						default:
							$("#content > div.row").attr('class', 'row').addClass(controller + ' ' + type).html(html).effect(effect, effect_options, effect_speed);
							break;
					}
					
					check_function_exist('bootstrap_contextmenu');
				}, "html");
				break;
			case 1:
				$.post(base_url + "index.php/" + controller + "/common", { type: type, id: ajax_post_param['id'], request: 'sub_page' },
				function (html) {
					window.location.hash = '#' + type + '&sub_page&' + ajax_post_param['id'];
					
					$("#content > div.row").html(html).effect('slide', '', effect_speed);
					
					check_function_exist('bootstrap_contextmenu');
				}, "html");
				break;
			case 2:
				$.post(base_url + "index.php/" + controller + "/common", { type: type, year: ajax_post_param['year'], request: 'sub_page' },
				function (html) {
					window.location.hash = '#' + type + '&sub_page&' + ajax_post_param['year'];
					
					$("#content > div.row").html(html).effect(effect, '', effect_speed);
					
					check_function_exist('bootstrap_contextmenu');
				}, "html");
				break;
			case 3:
				$.post(base_url + "index.php/" + controller + "/common", { type: type, year: ajax_post_param['year'], request: 'sub_page' },
				function (html) {
					window.location.hash = '#' + type + '&sub_page&' + ajax_post_param['year'];
					
					$("#content > div.row").html(html).effect(effect, effect_options, effect_speed);
					
					check_function_exist('bootstrap_contextmenu');
				}, "html");
				break;
			case 4:
				$.post(base_url + "index.php/about_us/common", { type: 'manager_community', request: 'sub_page' },
				function (html) {
					window.location.hash = '#manager_community&sub_page';
					
					$().containerSimulator({ destory: true });
					$("body").addClass('manager_community');
					
					controller = 'about_us';
					type = 'manager_community';
					
					if ($("body > div.my-container").length == 0) {
						$("body > div.container").attr('class', 'my-container');
					}
					
					$("body > div.my-container").html(html).effect(effect, '', effect_speed);
					
					check_function_exist('bootstrap_contextmenu');
				}, "html");
				break;
			case 5:
				window.location.hash = '#' + type + '&' + ajax_post_param['request'] + '&' + ajax_post_param['sub_page_type'];
				
				$("#manager_community_modal").css('display','none');
				$.post(base_url + "index.php/about_us/common", { type: 'manager_community', sub_page_type: ajax_post_param['sub_page_type'], request: ajax_post_param['request'] },
				function (html) {
					$("body > div.my-container").html(html).effect(effect, '', effect_speed);
					
					check_function_exist('bootstrap_contextmenu');
				}, "html");
				break;
			case 6:
				$("#manager_community_modal").css('display','none');
				$.post(base_url + "index.php/" + controller + "/common", { type: 'manager_community', id: ajax_post_param['id'], sub_page_type: ajax_post_param['sub_page_type'], request: 'flip_show_detail' },
				function (html) {
					$("#manager_community_modal").addClass('long-height');
					
					if (ajax_post_param['sub_page_type'] == 'design') {
						$("#manager_community_modal").removeClass('long-height');
					}
					
					$("#manager_community_modal").html(html).fadeIn('600');
					
					check_function_exist('bootstrap_contextmenu');
				}, "html");
				break;
		}
	}
    
$(document).ready(function() {
	if (controller != 'academy') {
		var temp_height = ($(".center-content").height()) ? $(".center-content").height() : $("div.container > #content > div.row").height();
		
		var margin_top = ($(document).height() - $("div.container > #header").height() - temp_height - $("#footer").height()) / 2;
		$("div.container > #content").css('margin-top', margin_top);
		//alert(margin_top);
	} else {
		$("div.container > #content").css('margin-top', '49px');
	}
	
	if ($("#menu .child ul li.active").attr('class')) {
		type = $("#menu .child ul li.active").attr('class').split(' ')[0];
		window.location.hash = '#' + type;
	}
	
	check_function_exist('bootstrap_contextmenu');
	
    $("#content > div.row").effect('slide', '', effect_speed);
    
	$("#menu .child ul li").click(function() {
		var pre_menu_tab_index = $(this).siblings('.active').index();
		var now_menu_tab_index = $(this).index();
		
		var options = null;
		if (pre_menu_tab_index < now_menu_tab_index && pre_menu_tab_index != -1) {
			effect_options = { direction: 'left' };
		} else {
			effect_options = { direction: 'right' };
		}
		
		$(this).siblings().removeClass('active');
		type = $(this).attr('class').split(' ')[0];
		$(this).addClass('active');
		
		// make it possible to reload previous tab
		$.cookie('current_type', type);
		
		ajax_post_param['type'] = type;
		do_ajax_post_by_condition(0, ajax_post_param);
	});
	
	/*$("body").ajaxStart(function(){
		$('.span9').spin('large');
	});*/
	
	$("#content div.index #dashboard .dashboard_button").live('click', function() {
		$(this).css({ 'background': '#B30001' });
		window.location.href = $('#header #menu .parent a.' + $(this).attr('id')).attr('href');
	});
		
	$("#content div.about_us.design_team .gallery #caption span").live('click', function() {
		ajax_post_param['id'] = $(this).attr('id');
		do_ajax_post_by_condition(1, ajax_post_param);
	});
	
	$("#content div.about_us.events #events_carousel li .events_content").live('click', function() {
		ajax_post_param['year'] = $(this).prev('div.year')[0].innerHTML;
		do_ajax_post_by_condition(2, ajax_post_param);
	});
	
	$("#content div.about_us.events .carousel-controller").live('click', function() {
		ajax_post_param['year'] = $(this).attr('id');
		
		if ($(this).hasClass('next')) {
			effect_options = { direction: 'left' };
		} else {
			effect_options = { direction: 'right' };
		}
		
		do_ajax_post_by_condition(3, ajax_post_param);
	});

	$("#content div.about_us.manager_community .portrait img, #content div.about_us.manager_community .portrait #caption, #footer div.friend-links #manager_community").live('click', function() {
		do_ajax_post_by_condition(4, ajax_post_param);
	});
	
	$("body.manager_community .my-container .sub_page div#flip_entrance div").live('click', function() {
		ajax_post_param['sub_page_type'] = $(this).attr('id');
		ajax_post_param['request'] = ($(this).hasClass('go-sub-page')) ? 'flip_sub_page' : 'flip_show_detail';
		do_ajax_post_by_condition(5, ajax_post_param);
	});
	
	$("body.manager_community .my-container .flip_sub_page .flip-block:not(.center)").live('click', function() {		
		if ($(this).children("div.title").hasClass('design')) {
			ajax_post_param['sub_page_type'] = 'design';
		} else if ($(this).children("div.title").hasClass('teach')) {
			ajax_post_param['sub_page_type'] = 'teach';
		} else if ($(this).children("div.title").hasClass('awards')) {
			ajax_post_param['sub_page_type'] = 'awards';
		} else if ($(this).children("div.title").hasClass('treatise')) {
			ajax_post_param['sub_page_type'] = 'treatise';
		}
		
		ajax_post_param['id'] = $(this).attr('id');
		do_ajax_post_by_condition(6, ajax_post_param);
	});
	
	$("#content div.media.common span.detail-plus").live('click', function() {
		$("#media_modal").css('display','none');
		$.post(base_url + "index.php/" + controller + "/common", { type: type, id: $(this).attr('id'), request: 'sub_page' },
		function (html) {
			$("#media_modal").html(html).fadeIn('600');
			
			check_function_exist('bootstrap_contextmenu');
		}, "html");
	});
	
	$("#content div.academy.course span.detail-plus").live('click', function() {
		$("#course_modal").css('display','none');
		$.post(base_url + "index.php/" + controller + "/common", { type: type, id: $(this).attr('id'), request: 'sub_page' },
		function (html) {
			$("#course_modal").html(html).fadeIn('600');
			
			check_function_exist('bootstrap_contextmenu');
		}, "html");
	});
	
	$("#content div.academy.exchange span.detail-plus").live('click', function() {
		$.post(base_url + "index.php/" + controller + "/common", { type: type, id: $(this).attr('id'), request: 'sub_page' },
		function (html) {
			$("#content > div.row").html(html).effect(effect, '', effect_speed);
			
			check_function_exist('bootstrap_contextmenu');
		}, "html");
	});
	
	$("#content div.join_us.staff_activities span.detail-plus").live('click', function() {
		$.post(base_url + "index.php/" + controller + "/common", { type: type, id: $(this).attr('id'), request: 'sub_page' },
		function (html) {
			$("#activity_color_box").html(html);
			
			check_function_exist('bootstrap_contextmenu');
		}, "html");
	});
	
	$(".feedback #submit").live('click', function() {
		var data = $(".feedback form").serializeArray();
		
		if (!validator(data)) {
			return;
		}
		
		$(".feedback #submit").html('请稍候...');
		
		$.post(base_url + "index.php/mailer", { data: data },
		function (res_data) {
			$(".feedback form input").val('');
			$(".feedback form textarea").val($(".feedback form textarea")[0].defaultValue);
			$(".feedback #submit").html('发 送');
			alert('您的留言已成功提交');
		}, "html");
		
	});
	
    $('#operation_modal').on('hide', function () {
    	if (editor) {
    		editor.destroy();
            editor = null;
    	}
	});
    
    // reload previous tab
    if ($.cookie('current_type') != '' && !$("." + $.cookie('current_type')).hasClass('active')) {
    	setTimeout('$("." + $.cookie(\'current_type\')).trigger(\'click\');', 1000);
    }
    
    // some redirect
    if ($.cookie('redirect') != '' && $.cookie('redirect') != $.cookie('current_type')) {
    	if ($.cookie('redirect') != 'manager_community') {
    		setTimeout('$("." + $.cookie(\'redirect\')).trigger(\'click\'); $.cookie(\'redirect\', \'\');', 1000);
    	} else {
    		setTimeout('$("#" + $.cookie(\'redirect\')).trigger(\'click\'); $.cookie(\'redirect\', \'\');', 1000);
    	}
    }
});
